Ext.define('Admin.view.archive.UserController', {
	        extend : 'Admin.app.CrudController',

	        alias : 'controller.user',

	        onRefreshDefault : function(view, record, params) {
		        var me = this, store = me.getStore('userStore');
		        store.getProxy().extraParams = {
			        type : Ext.util.Format.lowercase(record.data.level),
			        org : record.data.id
		        }
		        store.reload();
	        },

	        onInsertUserClick : function(btn) {
		        var me = this, grid = btn.ownerCt.ownerCt, region = me.getViewModel().get('record');
		        if (region) {
			        var record = Ext.create(grid.getStore().model, {
				                name : '新建用户',
				                region : region.data.id
			                });
			        record.store = grid.getStore();
			        me.showDetail(grid.detail, record);
		        }
	        },

	        onUserRowClick : function(table, record) {
		        var me = this, store = me.getStore('userRoles'), vm = me.getViewModel();
		        var userroles = me.getView().getReferences().userroles;
		        userroles.setTitle(record.data.username + '的所属角色');

		        store.getProxy().extraParams = {
			        user : record.data.id
		        };
		        store.reload();
	        },

	        onUserRoleCheck : function(column, rowIndex, checked, record, eOpts) {
		        var me = this, store = me.getStore('userRoles'), state = true;
		        var user = store.getProxy().extraParams.user, record = store.getAt(rowIndex);
		        Admin.Ajax.request({
			                async : false,
			                method : checked ? 'POST' : 'DELETE',
			                url : Ext.String.format('rest/api/user/{0}/roles', user),
			                params : {
				                role : [record.data.id]
			                },
			                fnFailure : function(options, result, status) {
				                Admin.Ajax.onFailure(options, result, status)
				                state = false;
			                }
		                });
		        return state;
	        },

	        onResetPasswordClick : function(button) {
		        var me = this, grid = me.getReferences().userlist, records = grid.getSelection(), i, len = records.length;
		        if (0 < len) {
			        Admin.Msg.confirm('确认要重置所有选中用户的密码吗？', function(buttonId) {
				                if ('yes' === buttonId) {
					                for (i = 0; i < len; i++) {
						                Admin.Ajax.request({
							                        method : 'POST',
							                        idx : i,
							                        url : Ext.String.format('rest/api/user/{0}/resetpswd',
							                                records[i].data.id),
							                        fnSuccess : function(option, result) {
								                        if (option.idx === (len - 1)) {
									                        Admin.Ajax.toast('选中用户的密码重置为“888888”，请及时通知用户登录并修改密码。');
								                        }
							                        }
						                        });
					                }
				                }
			                });
		        }
	        }

        });